/**
 * 
 */
package com.xb.cloud.front.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xb.cloud.front.common.FrontShopService;
import com.xb.cloud.gateway.client.ReturnCodes;
import com.xb.cloud.gateway.client.ReturnUtils;
import com.xb.cloud.gateway.client.RtnInfo;
import com.xb.cloud.product.category.service.client.bo.SkuRequestBO;
import com.xb.cloud.product.merchant.service.client.ShopNavigationGroupService;
import com.xb.cloud.product.merchant.service.client.ShopService;
import com.xb.cloud.product.service.business.model.ShopPromotionSku;
import com.xb.cloud.product.service.client.SkuService;
import com.xb.cloud.product.service.client.bo.ProductSkuPriceBo;
import com.xb.cloud.product.service.client.bo.SkuDetailBO;
import com.xb.cloud.service.client.ServiceReturnInfo;
import com.xb.cloud.service.client.ServiceReturnUtils;

/**
 * 前端店铺的服务类
 * 
 * @author kexue
 *
 */
@Service
public class FrontShopServiceImpl implements FrontShopService {

	@Resource
	private ShopService shopService;
	
	@Resource
	private ShopNavigationGroupService shopNavigationGroupService;

	@Resource
	private SkuService skuService;
	
	/**
	 * 根据shop id 查询店铺的详细信息
	 */
	@Override
	public RtnInfo getShopBoById(Long id) {
		ServiceReturnInfo returnInfo = shopService.getShopBoById(id);
		if (ServiceReturnUtils.isSuccess(returnInfo)) {
			return ReturnUtils.success(returnInfo.getData());
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
	}
	
	/**
	 * 查看某个店铺分组树形数据(由小到大)
	 * 
	 * @param Long
	 *            endMerchantUserId
	 * @return
	 */
	public RtnInfo getShopNavicationGroup(Long shopId) {
		ServiceReturnInfo rtnInfo = shopNavigationGroupService.getShopNavicationGroup(shopId);
		if (ServiceReturnUtils.isSuccess(rtnInfo)) {
			return ReturnUtils.success(rtnInfo.getData());
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
	}

	/**
	 * 获取当前店铺的推荐列表
	 */
	@Override
	public RtnInfo getShopPromotionSkuList(Long shopId) {
		//获取推荐列表（只有id）
		ServiceReturnInfo idsInfo =shopService.getShopPromotionSkuList(shopId);
		if (!ServiceReturnUtils.isSuccess(idsInfo)) {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
		@SuppressWarnings("unchecked")
		List<ShopPromotionSku> ids=(List<ShopPromotionSku>)idsInfo.getData();
		//根据推荐sku的id获取详细信息（包括价格）
		List<HashMap<String,Object>> result=new ArrayList<HashMap<String,Object>>();
		HashMap<String,Object> item=null;
		for(ShopPromotionSku shopPromotionSku:ids){
			item=new HashMap<String,Object>();
			//设置skud
			Long skuId=shopPromotionSku.getSkuId();
			item.put("id", skuId);
			item.put("shopId", shopId);
			//设置sku名字，规格，图标
			SkuRequestBO requestBO=new SkuRequestBO();
			requestBO.setSkuID(skuId);
			ServiceReturnInfo detailInfo=skuService.getSKUDetail(requestBO);
			SkuDetailBO detailBo=(SkuDetailBO) detailInfo.getData();
			item.put("skuNameCn", detailBo.getName());
			item.put("productUnit",detailBo.getUnit());
			item.put("picUrl", detailBo.getPictureURLList().get(0));
			//设置sku价格
			ServiceReturnInfo priceInfo = skuService.getSKUPrice(requestBO);
			ProductSkuPriceBo priceBO=(ProductSkuPriceBo) priceInfo.getData();
			item.put("priceSale", priceBO.getPriceSale());
			//接入结果集
			result.add(item);
		}
		return ReturnUtils.success(result);
	}

}
